<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="昵称" prop="nickname">
          <el-input
            v-model="queryParams.nickname"
            placeholder="请输入昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['ai:wechatUser:edit']"
          >修改</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="wechatUserList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id"  width="55"/>
        <el-table-column label="昵称" align="center" prop="nickname" width="150" />
        <el-table-column label="用户名" align="center" prop="username"/>
        <el-table-column label="是否关闭" align="center" width="100"  prop="close" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

          <template slot-scope="scope">
            <el-button v-if="false"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['ai:wechatUser:edit']"
            >修改</el-button>

            <el-button
              size="mini"
              type="text"
              @click="checkLog(scope.row)"
              v-hasPermi="['ai:wechatLog:list']"
            >聊天记录</el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['ai:wechatUser:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改插件列对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="昵称">
            <el-input disabled v-model="form.nickname" />
          </el-form-item>
          <el-form-item label="用户名">
            <el-input placeholder="请输入" v-model="form.username" disabled />
          </el-form-item>
          <el-form-item label="是否关闭">
            <el-input placeholder="请输入" v-model="form.close"/>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 聊天记录 -->
      <el-dialog title="聊天记录" :visible.sync="logOpen" width="80%" append-to-body :close-on-click-modal="false">
        <el-table v-loading="logLoading" :data="wechatLogList" height="500px" :row-class-name="tableRowClassName">
          <el-table-column label="类型" align="center" prop="type"  width="100"/>
          <el-table-column label="内容" align="center" prop="content"/>
          <el-table-column label="时间" align="center" width="200"  prop="createTime" />
        </el-table>

        <pagination
          v-show="logTotal>0"
          :total="logTotal"
          :page.sync="logQueryParams.pageNum"
          :limit.sync="logQueryParams.pageSize"
          @pagination="getLogList"
        />
      </el-dialog>

    </div>
  </template>

  <script>
  import { listWechatUser, getWechatUser, delWechatUser, addWechatUser, updateWechatUser } from "@/api/ai/wechat_user";
  import { listWechatLog } from '@/api/ai/wechat_log'

  export default {
    name: "wechat_user",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 插件列表格数据
        wechatUserList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
        },
        //聊天记录
        wechatLogList:[],
        logTotal:0,
        logQueryParams:{
          pageNum: 1,
          pageSize: 10,
        },
        logLoading:false,
        logOpen:false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          question: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      tableRowClassName(row,rowIndex){
        if (row.row.type == 'human') {
          console.log('human')
          return 'warning-row';
        } else if (row.row.type == 'ai') {
          console.log('ai')
          return 'success-row';
        }
        console.log(row)
        return '';
      },
      getPathList(e){
        this.form.path = e[0].path
      },
      /** 查询插件列列表 */
      getList() {
        this.loading = true;
        listWechatUser(this.queryParams).then(response => {
          this.wechatUserList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getLogList(){
        this.logLoading = true
        listWechatLog(this.logQueryParams).then(response => {
          this.wechatLogList = response.rows
          this.logTotal = response.total
          this.logLoading=false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          nickname: null,
          username: null,
          close:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加插件列";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWechatUser(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改插件列";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWechatUser(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWechatUser(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除插件列编号为"' + ids + '"的数据项？').then(function() {
          return delWechatUser(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      checkLog(row){
        this.wechatLogList = []
        this.logQueryParams = {
          pageNum: 1,
          pageSize: 10,
          username:row.username
        }
        this.logOpen=true
        this.getLogList()
      },
    }
  };
  </script>

<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
